Some matplotlib exercises


In [1]:
from matplotlib.backends.backend_pdf import PdfPages
pp = PdfPages('exercises.pdf')

In [2]:
# imports
%matplotlib inline
import matplotlib.pyplot as plt
import numpy as np

In [3]:
# Simple graph, y = x
x = np.linspace(-10, 10)
y = x
plt.plot(x, y, label=r'$y=x$')
plt.legend(loc='best')
pp.savefig()



In [4]:
# Simple graph, y = x, with grid and line style
x = np.linspace(-10, 10)
y = x
plt.plot(x, y, '--or', label=r'$y=x$')
plt.legend(loc='best')
plt.grid()
pp.savefig()



In [5]:
# Simple graph, y = sin(x), with grid and line style
x = np.linspace(-10, 10)
y = np.sin(x)
plt.plot(x, y, '--.', label=r'$y=\sin{x}$')
plt.legend(loc='best')
plt.grid()
pp.savefig()



In [6]:
# Simple graph, y = sin(x**2), with grid and line style
x = np.linspace(-10, 10, 1000)
y = np.sin(x ** 2)
plt.plot(x, y, '-g', label=r'$y=\sin{x^2}$')
plt.legend(loc='best')
plt.grid()
pp.savefig()



In [7]:
# Simple graph, y = sin(x) ** 2, sin(x), with grid and line style
x = np.linspace(-10, 10, 1000)
y = np.sin(x) ** 2
y2 = np.sin(x)
plt.plot(x, y2, '--', label=r'$y=\sin{x}$')
plt.plot(x, y, '-g', label=r'$y=\sin^2{x}$')
plt.legend(loc='best')
plt.grid()
pp.savefig()



In [8]:
# physics graph, x = x_0 + vt, x_0 = 0 v = 2, with grid and line style
time = np.linspace(0, 10, 1000)
pos1 = 0 + 2 * time
pos2 = 2 - 3 * time
plt.plot(time, pos1, '-', label=r'$x=x_0+vt;\,x_0=0,v=2$')
plt.plot(time, pos2, '-', label=r'$x=x_0+vt;\,x_0=2,v=-3$')
plt.legend(loc='best')
plt.xlabel('time [s]')
plt.ylabel('position [m]')
plt.grid()
pp.savefig()



In [9]:
# physics graph, x = x_0 + vt + 1/2 at**2, x_0 = 0 v = 2, with grid and line style
time = np.linspace(0, 10, 1000)
pos1 = 0 + 2 * time - 1 * time ** 2
pos2 = 2 - 3 * time + 1.5 * time ** 2
plt.plot(time, pos1, '-', label=r'$x=x_0+vt+0.5at^2;\,x_0=0,v=2,a=-2$')
plt.plot(time, pos2, '-', label=r'$x=x_0+vt+0.5at^2;\,x_0=2,v=-3,a=3$')
plt.legend(loc='best')
plt.xlabel('time [s]')
plt.ylabel('position [m]')
plt.grid()
pp.savefig()



In [10]:
# physics graph, x = x_0 + vt + 1/2 at**2, x_0 = 0 v = 2, with grid and line style
# inset
time = np.linspace(0, 10, 1000)
pos1 = 0 + 2 * time - 1 * time ** 2
pos2 = 2 - 3 * time + 1.5 * time ** 2
plt.plot(time, pos1, '-', label=r'$x=x_0+vt+0.5at^2;\,x_0=0,v=2,a=-2$')
plt.plot(time, pos2, '-', label=r'$x=x_0+vt+0.5at^2;\,x_0=2,v=-3,a=3$')
plt.legend(loc=3)
plt.xlabel('time [s]')
plt.ylabel('position [m]')
plt.grid()

time = np.linspace(0, 2, 1000)
pos1 = 0 + 2 * time - 1 * time ** 2
pos2 = 2 - 3 * time + 1.5 * time ** 2
plt.axes([.25, .65, .2, .2])
plt.plot(time, pos1, '-', label=r'$x=x_0+vt+0.5at^2;\,x_0=0,v=2,a=-2$')
plt.plot(time, pos2, '-', label=r'$x=x_0+vt+0.5at^2;\,x_0=2,v=-3,a=3$')
plt.xlabel('time [s]')
plt.ylabel('position [m]')
plt.grid()

pp.savefig()



In [11]:
# physics graph, T = A * D ** n; A = 2 * pi / sqrt(g), n = 0.5

diameter = np.linspace(0, 1, 100)
A = 2 * np.pi / np.sqrt(9.8)
period = A * diameter ** 0.5
plt.loglog(diameter, period, '-', label=r'$T=AD^n$')
plt.legend(loc='best')
plt.xlabel('diameter [m]')
plt.ylabel('period [s]')
plt.grid(which='minor')
plt.grid(which='major')
pp.savefig()



In [12]:
# Random distribution plots
x = np.random.normal(0, 1, size=10000)
y = np.random.normal(0, 1, size=10000)
z = np.random.normal(0, 1, size=10000)

plt.figure()
plt.subplot(221)
plt.hist(x, bins=20, normed=True)
plt.xlabel(r'$x$')
plt.ylabel(r'$P(x)$')
plt.subplot(222)
plt.hist(y, bins=20, normed=True, color='green')
plt.xlabel(r'$y$')
plt.ylabel(r'$P(y)$')
plt.subplot(223)
plt.hist(z, bins=20, normed=True, color='red')
plt.xlabel(r'$z$')
plt.ylabel(r'$P(z)$')
plt.subplot(224)
plt.hist(np.sqrt(x*x + y*y + z*z), bins=20, normed=True, color='cyan')
plt.xlabel(r'$\sqrt{x^2 + y^2 + z^2}$')
plt.ylabel(r'$P(\sqrt{x^2 + y^2 + z^2})$')
plt.tight_layout()
pp.savefig()



In [5]:
x = np.linspace(0, 4 * np.pi, 1000)
y1 = np.cos(x)
y2 = np.sin(x)
y3 = x ** 2 + np.sin(x)

plt.figure()

plt.subplot(221)
plt.plot(x, y1, 'r', label=r'$y = \sin x$')
plt.grid()
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc='upper right')

plt.subplot(223)
plt.plot(x, y2, 'g', label=r'$y = \cos x$')
plt.grid()
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc='upper right')

plt.subplot(122)
plt.plot(x, y3, 'b', label=r'$y = x ^ 2 + \sin x$')
plt.grid()
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc='upper right')

plt.tight_layout()
# pp.savefig()



In [14]:
x = np.linspace(0, 8 * np.pi, 1000)
y1 = np.sin(0.3 * x) + np.sin(x)
y2 = np.sin(3.0 * x) + np.sin(x)

plt.figure()

plt.subplot(211)
plt.plot(x, y1, 'r', label=r'$y = \sin {{(0.3 x)}} + \sin x$')
plt.grid()
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc='upper right')

plt.subplot(212)
plt.plot(x, y2, 'g', label=r'$y = \sin {{(3.0 x)}} + \sin x$')
plt.grid()
plt.xlabel('x')
plt.ylabel('y')
plt.legend(loc='upper right')

plt.tight_layout()
pp.savefig()



In [15]:
# Multi page pdf save
pp.close()

In [ ]: